package core;


import javax.swing.DefaultListModel;
import javax.swing.SwingUtilities;

import util.*;

public class NetworkReader {
	
	private DefaultListModel<String> infoModel = new DefaultListModel<String>();
	private DefaultListModel<Order> orderModel = new DefaultListModel<Order>();
	NetClient tc;
	
	public NetworkReader(NetClient tc) {
		this.tc = tc;
	}
	
	public void update(final String msg) {
		SwingUtilities.invokeLater( new Runnable() {
			@Override
			public void run() {
				infoModel.addElement(msg);
				String[] s = msg.split(";");
				String command = s[0];
				
				if (command.equals("MATCH")){
					int id = Integer.parseInt(s[1]);
					String name = s[2];
					float price = Float.parseFloat(s[3]);
					int amount = (int)Float.parseFloat(s[4]);
					
					FillReport report = new FillReport(name, price, amount);
					
					Order order = orderModel.get(id);
					order.addReport(report);
					orderModel.set(id, order);
				
				} else if (command.equals("CANCEL") ){
					if (s[2].equals("SUCCESSFUL") ){
						int id = Integer.parseInt(s[1]);
						
						Order order = orderModel.get(id);
						order.setOrderStatus("cancelled");
						orderModel.set(id, order);
					}
					
				}
			}
		});
	}

	public DefaultListModel<String> getInfoModel() {
		return infoModel;
	}
	
	public DefaultListModel<Order> getOrderModel() {
		return orderModel;
	}
	
}
